package TetrisFlash.Objects.Tetrominoes 
{
	import TetrisFlash.Objects.TetrisBoard.TetrisBoard;
	import starling.display.QuadBatch;
	import starling.display.Image;
	import TetrisFlash.GlobalData.Global;
	import TetrisFlash.Screen.Screen;
	import TetrisFlash.GameLoadData.AssetsTexture;
	/**
	 * ...
	 * @author DANTRUONG
	 */
	public class ITetromino 
	{
		//Position of tetromino by board - NOTE: BY BOARD...not screen
		public var posX:int, posY:int;
		
		//Old position of tetromino by board
		public var oldPosX:int, oldPosY:int;
		
		//Column and row of array tetromino
		public var column:int, row:int;
		
		//Array of tetromino
		public var tetrominoArray:Array;
		
		//int ID
		public var tetrominoID:int;
		
		//screen dislay this tetromino
		public var screen:Screen;
		
		//QuadBatch store and render tetromino
		public var tetrominoQuadBatch:QuadBatch;
		
		//image element of tetromino
		public var tetrominoImage:Image;
		
		public function ITetromino(_screen: Screen) 
		{
			//Position start
			posX = 4;
			posY = -2;
			oldPosX = posX;
			oldPosY = posY;
			
			//init screen from parameter
			screen = _screen;
			
			//init Array tetromino
			tetrominoArray = new Array();
			for (var i:int = 0; i < column; i++)
			{
				tetrominoArray[i] = new Array();
			}
		}
		
		//init resource image and create a null QuadBatch
		public function init():void {
			if(tetrominoID != 0){
				tetrominoImage = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_" + tetrominoID));
			}
			tetrominoQuadBatch = new QuadBatch();
		}
		
		//Check collision with border and board with go Dx (left: -1, right: 1) or Dy(up: -1, down: 1)
		public function isCollision (tBoard: TetrisBoard, Dx:int = 0, Dy: int = 0): Boolean {
			for (var i:int = 0; i < column; i++) {
				for (var j:int = 0; j < row; j++) {
					if (tetrominoArray[i][j]) {
						//Collision with border
						if (((posX + i + Dx) < 0) || ((posX+i+Dx) >= tBoard.column) ||
						((posY + j + Dy) >= tBoard.row)) {
							return true;
						}
						//Collisiom with board
						if (tBoard.boardArray[posX + i + Dx][posY + j + Dy]) {
							return true;
						}
					}
				}
			}
			return false;
		}
		
		//Move tetromino
		public function isMoveLeftOK (tBoard: TetrisBoard): Boolean {
			if (!isCollision(tBoard, -1)) {
				posX--;
				return true;
			}
			return false;
		}
		
		public function isMoveRightOK (tBoard: TetrisBoard): Boolean {
			if (!isCollision(tBoard, 1)) {
				posX++;
				return true;
			}
			return false;
		}
		
		public function isMoveDownOK (tBoard: TetrisBoard): Boolean {
			if (!isCollision(tBoard, 0, 1)) {
				posY++;
				return true;
			}
			return false;
		}
		
		//Rotation then add offset position Dx and Dy (Dx, Dy is center of rota)
		public function isRotation(tBoard: TetrisBoard, Dx:int = 0, Dy:int = 0): Boolean {
			//Create a temp tetromino
			var tempTetromino: ITetromino = new ITetromino(screen);
			tempTetromino.tetrominoID = tetrominoID;
			tempTetromino.init();
			
			//Create a team array
			var tempArray:Array = new Array(row);
			for (var i:int = 0; i < row; i++)
			{
				tempArray[i] = new Array(column);
			}
			
			//Rotation matrix
			for (var k: int = 0; k < column; k++) {
				for (var j: int = 0; j < row; j++) {
					tempArray[row - (j + 1)][k] = tetrominoArray[k][j];
				}
			}
			
			//Copy data from this to temp tetromino with change row column
			tempTetromino.tetrominoArray = tempArray;
			tempTetromino.posX = posX;
			tempTetromino.posY = posY;
			tempTetromino.column = row;
			tempTetromino.row = column;
			
			//check collision temp tetro with the board
			if (!tempTetromino.isCollision(tBoard, Dx, Dy)) {
				//free array
				tetrominoArray = []; // or tetrominoArray.length = 0; or tetrominoArray = new Array();
				
				tetrominoArray = tempArray;
				row = tempTetromino.row;
				column = tempTetromino.column;
				posX += Dx;
				posY += Dy;
				return true;
			}else {
				return false;
			}
		}
		
		//Draw this tetromino
		public function drawTetromino():void {
			//add image to quadbatch
			for (var i: int = 0; i < column; i++) {
				for (var j: int = 0; j < row; j++) {
						if (tetrominoArray[i][j]) {
							if (posY + j >= 2) {
								tetrominoImage.x = Global.OFFSET_TOBOARD_X + (posX + i) * Global.CELL_WIDTH;
								tetrominoImage.y = Global.OFFSET_TOBOARD_Y + (posY + j - 2) * Global.CELL_WIDTH;
								tetrominoQuadBatch.addImage(tetrominoImage);
								tetrominoImage.dispose();
							}
						}
					
				}
			}
		}
		
		//Draw this tetromino with draw game loop
		public function updateDrawTetromino():void {
			//if screen not contain quadbatch then add quadbatch
			if(!screen.contains(tetrominoQuadBatch)){
				screen.addChild(tetrominoQuadBatch);
			}
			
			//reset all data of tetromnimo but not dispose
			tetrominoQuadBatch.reset();
			
			//add data for quadbatch
			drawTetromino();
		}
		
		//Draw tetromino with ID and position by screen
		public function drawTetrominoXY(ID:int, X: int, Y: int):void {
			//init image follow ID
			tetrominoImage = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_" + ID));
			//add image to quadbatch
			for (var i: int = 0; i < column; i++) {
				for (var j: int = 0; j < row; j++) {
						if (tetrominoArray[i][j]) {
								tetrominoImage.x = X + i * Global.CELL_WIDTH;
								tetrominoImage.y = Y + j * Global.CELL_WIDTH;
								tetrominoQuadBatch.addImage(tetrominoImage);
								tetrominoImage.dispose();
						}
					
				}
			}
		}
		
		//Draw tetromino with ID and position by screen with draw game loop
		public function updateDrawTetrominoXY(ID:int, X: int, Y: int):void {
			//if screen not contain quadbatch then add quadbatch
			if(!screen.contains(tetrominoQuadBatch)){
				screen.addChild(tetrominoQuadBatch);
			}
			
			//reset all data of tetromnimo but not dispose
			tetrominoQuadBatch.reset();
			
			//add data for quadbatch
			drawTetrominoXY(ID, X, Y);
		}
		
		public function updatePositionQuadBatch():void {
			tetrominoQuadBatch.x += (posX - oldPosX) * Global.CELL_WIDTH;
			tetrominoQuadBatch.y += (posY - oldPosY) * Global.CELL_WIDTH;
			oldPosX = posX;
			oldPosY = posY;
		}
		
		public function release():void {
			tetrominoArray.length = 0;
			if (screen.contains(tetrominoQuadBatch)) {
				screen.removeChild(tetrominoQuadBatch);
			}
			tetrominoImage.dispose();
			tetrominoQuadBatch.dispose();
		}
	}

}